//VML Adapter namespace
squigle.vmlAdapter = {};

squigle.vmlAdapter._stroke = function(stroke){
    var node = document.createElement("v:stroke");
    node.setAttribute("color", stroke.color());
    node.setAttribute("opacity", stroke.opacity() + "%");
    node.setAttribute("weight", stroke.size() + "px");    
    node.setAttribute("endcap", stroke.endcap());
    node.setAttribute("joinstyle", stroke.linejoin());
    node.setAttribute("dashstyle", stroke.dash());  
    return node;
};

squigle.vmlAdapter._fill = function(fill){
    var node = document.createElement("v:fill");
    node.setAttribute("color", fill.color());
    node.setAttribute("opacity", fill.opacity() + "%");
    return node;
};

/**
 * We need to be sure the v namespace for vml is available before we create any nodes
 * So this will be done at load time
 * Comment from Chris: This is a trade off between not having load time code (nicer looking / more predictable) and having a simple API
 * where the user doesn't need to call an init() event before they use anything in the API. This should be reviewed again before the code
 * goes beta.
 */
if(document.namespaces && !document.namespaces.olv) {
    document.namespaces.add("v", "urn:schemas-microsoft-com:vml");
    var style = document.createStyleSheet();
    style.addRule('v\\:*', "behavior: url(#default#VML); " + "position: absolute; display: inline-block;");
}

/**
 * Value Objects
 */
squigle.vmlAdapter.Point = function(x, y){    
    this.x = function(){
        if(squigle.preserveDecimals){
            return x * 65536 || 0;   
        }
        return Math.round(x) || 0;
    };
    
    this.y = function(){
        if(squigle.preserveDecimals){
            return y * 65536 || 0;   
        }
        return Math.round(y) || 0;
    }
};

squigle.vmlAdapter.Size = function(width, height){
    this.width = function(){
        if(squigle.preserveDecimals){
            return width * 65536; 
        }
        return Math.round(width);
    };
    
    this.height = function(){
        if(squigle.preserveDecimals){
            return height * 65536; 
        }
        return Math.round(height);
    };
};

squigle.vmlAdapter.scale = function(element, resolution, retainCenterPoint){
    var currentWidth = parseInt(element.style.width);
    var currentHeight = parseInt(element.style.height);
    var newWidth = currentWidth * resolution;
    var newHeight = currentHeight * resolution;
    if(retainCenterPoint){
        var currentLeft = parseInt(element.style.left);
        var currentTop = parseInt(element.style.top);
        element.style.left = currentLeft - ( (newWidth - currentWidth) / 2 ) + "px";
        element.style.top = currentTop - ( (newHeight - currentHeight) / 2 ) + "px";
    }
    alert(element.style.width);
    element.style.width = newWidth + "px";
    alert(element.style.width);
    element.style.height = newHeight + "px"; 
};

/**
 * Entities
 */
squigle.vmlAdapter.Canvas = function(size, coordSize, coordOrigin){
    var _nonBinaryScaleWidth = (squigle.preserveDecimals) ? size.width() / 65536 : size.width();
    var _nonBinaryScaleHeight = (squigle.preserveDecimals) ? size.height() / 65536 : size.height();
    var _container = document.createElement("div");
    _container.style.width = _nonBinaryScaleWidth + "px";
    _container.style.height = _nonBinaryScaleHeight + "px";
    
    var _clip = document.createElement("div");
    _clip.style.width = _nonBinaryScaleWidth + "px";
    _clip.style.height = _nonBinaryScaleHeight + "px";
    _clip.style.position = "absolute";
    _clip.style.clip = "rect(0px "+ _nonBinaryScaleWidth +"px "+ _nonBinaryScaleHeight +"px 0px)";
    
    //With SVG the width and height are always the same as the biggest value
    var _canvasDiameter = (coordSize.width() > coordSize.height()) ? coordSize.width() : coordSize.height();
    
    var _node = document.createElement("v:group");
    _node.style.width = _nonBinaryScaleWidth + "px"; 
    _node.style.height = _nonBinaryScaleHeight + "px";			
    _node.setAttribute("coordorigin", coordOrigin.x() + "," + coordOrigin.y());
    _node.setAttribute("coordsize", coordSize.width() + "," + coordSize.height());

    _container.appendChild(_clip);
    _clip.appendChild(_node);
    
    this.container = function(){
        return _container;
    };
    
    this.append = function(shape){
        _node.appendChild(shape.node());
    };
    
    this.node = function(){
        return _node;
    };
    
    this.coordSize = function(){
        return coordSize;
    };
    
    this.coordOrigin = function(){
        return coordOrigin;
    };
};

squigle.vmlAdapter.Rectangle = function(size, point, stroke, fill){
    var _node = (
        function(size){
            //Node
            var node = document.createElement("v:rect");
            node.style.setAttribute("width", size.width() + "px");
            node.style.setAttribute("height", size.height() + "px");
            node.style.position = "absolute";
            node.style.left = point.x() + "px";
            node.style.top = point.y() + "px";
            
            //Stroke / Fill
            stroke = stroke || new squigle.Stroke();
            fill = fill || new squigle.Fill();
            node.appendChild(squigle.vmlAdapter._stroke(stroke));
            node.appendChild(squigle.vmlAdapter._fill(fill));
            node.setAttribute("fillcolor", fill.color());
            return node;
        }
    )(size);
    
    this.node = function(){
        return _node;  
    };
    
    this.stroke = function(){
        return stroke;  
    };
};

squigle.vmlAdapter.Path = function(stroke, fill){
    var _path = "";
    var _currentPosition = new squigle.Point();
    
    var _node = (
        function(){
            var node = document.createElement("v:shape");
            
            node.style.position = "absolute";
            node.style.width = "100%";
            node.style.height = "100%";
            
            //Stroke / Fill
            stroke = stroke || new squigle.Stroke();
            fill = fill || new squigle.Fill();
            node.appendChild(squigle.vmlAdapter._stroke(stroke));
            node.appendChild(squigle.vmlAdapter._fill(fill));
            node.setAttribute("fillcolor", fill.color());
            return node;
        }
    )();
    
    this.node = function(){
        return _node;  
    };
    
    this.stroke = function(){
        return stroke;  
    };    
    
    this.moveTo = function(point, relative){
        var letter = (relative) ? "t" : "m";
        _path += " " + letter + " " + point.x() + "," + point.y();
        _node.setAttribute("path", _path);
        _currentPosition = point;
    };
    
    this.lineTo = function(point, relative){
        var letter = (relative) ? "r" : "l";
        _path += " " + letter + " " + point.x() + "," + point.y();
        _node.setAttribute("path", _path);
        _currentPosition = point;
    };
    
    this.curveTo = function(controlPoint1, controlPoint2, endPoint, relative){
         var letter = (relative) ? "v" : "c";
        _path += " " + letter + " " + controlPoint1.x() + "," + controlPoint1.y() + " " + controlPoint2.x() + "," + controlPoint2.y() + " " + endPoint.x() + "," + endPoint.y();
        _node.setAttribute("path", _path);
        _currentPosition = endPoint;
    };
    
    
    this.close = function(){
        _path += " x";
        _node.setAttribute("path", _path); 
    };
    
    this.clear = function(){};   
};